const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
//格式化时间，只保留年-月-日-小时
const formatYMDH = date=>{
  var date_Str = String(date);
  var date = new Date(date_Str.replace(/\-/g, "/") );
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = 0
  const second = 0
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatDate(date, formater) {
  if (date === null || date === undefined) {
    return '';
  }
  var date_Str = String(date);
  var date = new Date(date_Str.replace(/\-/g, "/") );

  formater = formater || 'yyyy-MM-dd hh:mm:ss';
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  //年月日
  const ymdList = [];
  //时分秒
  const hmsList = [];
  if (formater.indexOf('yyyy') > -1) {
    ymdList.push(year);
  }
  if (formater.indexOf('MM') > -1) {
    ymdList.push(month);
  }
  if (formater.indexOf('dd') > -1) {
    ymdList.push(day);
  }
  if (formater.indexOf('hh') > -1) {
    hmsList.push(hour);
  }
  if (formater.indexOf('mm') > -1) {
    hmsList.push(minute);
  }
  if (formater.indexOf('ss') > -1) {
    hmsList.push(second);
  }
  let result = '';
  if (ymdList.length > 0) {
    result += ymdList.map(formatNumber).join('-');
  }
  if (hmsList.length > 0) {
    result += ' ';
    result += hmsList.map(formatNumber).join(':');
  }
  return result;
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**手机号码校验 */
function checkTelphoneNumber(telphone) {
  // var reg = /^1[3|4|5|7|8][0-9]{9}$/; //验证规则
  // var reg = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57]|19[0-9]|16[0-9])[0-9]{8}$/;
  //手机号码，座机号码，400电话
  var reg = /(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[3456789]\d{9})$)|(^(400)-(\d{3})-(\d{4})(.)(\d{1,4})$)|(^(400)-(\d{3})-(\d{4}$))/;
  return reg.test(telphone);
}
function trim(s) {
  return s.replace(/(^\s*)|(\s*$)/g, "");
}
function isMoney(s) {
  //金额 允许正负数
  var reg = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
  return reg.test(s)
}
function isEmoji(str) {
  let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])|(\s)/g;
  return reg.test(str)
}

function checkDouble(number,length){
  length = length ||2;
  if(isNaN(number)){
    //不是数字
    return false;
  }
  let index = String(number).indexOf('.');
  if(index > -1){
    //小数位数超过检查长度
    if(String(number).length - (index+1)>length){
      return false;
    }
  }
  //整数
  return true;
}
//拨打电话
function callSomeBody(telphoneName) {
  if (this.checkTelphoneNumber(telphoneName)) {
    wx.makePhoneCall({
      phoneNumber: telphoneName,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
}

module.exports = {
  formatTime: formatTime,
  checkTelphoneNumber: checkTelphoneNumber,
  trim: trim,
  isMoney: isMoney,
  isEmoji: isEmoji,
  formatDate: formatDate,
  callSomeBody: callSomeBody,
  checkDouble:checkDouble,
  formatYMDH:formatYMDH
}